//
//  WWLoginViewController.m
//  WeiWeiHaircut
//
//  Created by 超群 on 15/10/8.
//  Copyright © 2015年 超群. All rights reserved.
//

#define sbGrayColor  [UIColor colorWithHexString:@"#a58f8d"]

#import "WWLoginViewController.h"
#import "MainTabBarViewController.h"
#import "WWRegisterViewController.h"
#import "WWFindPasswordViewController.h"
#import "QZUserModel.h"

@interface WWLoginViewController ()<QZRequestProtocol>

@end

@implementation WWLoginViewController{
    
    NSDictionary *_loginInfo;//登入信息
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self titleWithSting:@"威威理发"];
    [self buildUI];
    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hidenKeyBoard)];
    [self.view addGestureRecognizer:tap];
    
    [self checkInfo];
//    [self loginTap];
}
//免登录处理
-(void)checkInfo
{
    if([[TMCache sharedCache] objectForKey:@"loginInfo"]){
        MainTabBarViewController* mainVC = [[MainTabBarViewController alloc]initWithNibName:nil bundle:nil];
        [self presentViewController:mainVC animated:YES completion:nil];
    }
}
- (void)viewWillAppear:(BOOL)animated
{
    
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)hidenKeyBoard
{
    [self.view endEditing:YES];
}

- (void)buildUI
{
    self.view.layer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"f11f3a292df5e0fe6c2e1dfb5e6034a85edf7212.png"].CGImage);
    
    [self.loginButton addTarget:self action:@selector(loginTap) forControlEvents:UIControlEventTouchUpInside];
    [self.registerButton addTarget:self action:@selector(goRegister) forControlEvents:UIControlEventTouchUpInside];
    [self.findPasswordButton addTarget:self action:@selector(goFindPassword) forControlEvents:UIControlEventTouchUpInside];

    self.userNmaeTextField.textColor = [UIColor whiteColor];
    [self.userNmaeTextField setValue:[UIColor colorWithHexString:@"#907b75"] forKeyPath:@"_placeholderLabel.textColor"];
    [self.userNmaeTextField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];

    self.passWordTextField.textColor = [UIColor whiteColor];
    [self.passWordTextField setValue:[UIColor colorWithHexString:@"#907b75"] forKeyPath:@"_placeholderLabel.textColor"];
    [self.passWordTextField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
    

    
    self.loginButton.alpha           = 0.8;
    self.registerButton.alpha        = 0.8;
}

#pragma mark- 登入界面--
- (void)loginTap
{
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",newPublicURL,@"User.GetLogin"];
    QZRequest *request = [[QZRequest alloc]init];
    request.delegate = self;
    
    if (![_passWordTextField.text delSpace].length) {
        [[iToast makeText:@"请输入用户名"] show];
        return;
    }
    if (![_userNmaeTextField.text delSpace].length) {
        [[iToast makeText:@"请输入密码"] show];
        return;
    }
    
    
            NSDictionary *dic = @{@"username":[_userNmaeTextField.text delSpace],
                                  @"password" :[_passWordTextField.text delSpace],
                                  @"user_type":@(1)};
            _loginInfo = dic;
            [request QZRequest_POST:urlStr parameters:dic tagNSString:@"Register/logins" stopRequest:YES isSerializer:NO isCache:NO];
  

}
- (void)goRegister
{
    WWRegisterViewController* registerVC = [[WWRegisterViewController alloc]initWithNibName:nil bundle:nil];
    [self.navigationController pushViewController:registerVC animated:YES];
}

- (void)goFindPassword
{
    WWFindPasswordViewController* findPasswordVC = [[WWFindPasswordViewController alloc]initWithNibName:nil bundle:nil];
    [self.navigationController pushViewController:findPasswordVC animated:YES];
}
#pragma  mark QZRequestProtocol
-(void)request_POST_FinishValue:(id)value tagNSString:(NSString *)tag
{
    NSLog(@"value = %@",value);
    [[iToast makeText:value[@"info"]] show];
    if([tag isEqualToString:@"Register/logins"]){
      
        if ([value[@"data"][@"status"] intValue] == 100) {

            
            QZUserModel *model = [[QZUserModel alloc] initWithDictionary:value[@"data"][@"data"] error:nil];
            
            [QZUserInfo keepUserMsgWithUserModel:model];
            [[TMCache sharedCache] setObject:_loginInfo forKey:@"loginInfo"];
            MainTabBarViewController* mainVC = [[MainTabBarViewController alloc]initWithNibName:nil bundle:nil];
            [self presentViewController:mainVC animated:YES completion:nil];
        }else{
            [[iToast makeText:value[@"data"][@"info"]] show];
        }
    }
}
@end
